package com.tiancheng.onecode.controller.api.merchant;

import com.tiancheng.onecode.model.dto.PayMerchantDTO;
import com.tiancheng.onecode.model.dto.PlatformMerchantUserDetailDTO;
import com.tiancheng.onecode.model.request.admin.*;
import com.tiancheng.onecode.model.response.PageInfo;
import com.tiancheng.onecode.model.response.Result;
import com.tiancheng.onecode.model.response.admin.*;
import com.tiancheng.onecode.service.IMerchantService;
import com.tiancheng.onecode.utils.MerchantContext;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: kellen
 * @Description: 商户管理 admin 商户端 controller
 * @Date: create in 2024/12/9 21:27
 */
@Slf4j
@Controller
@RequestMapping("/api/b/merchant")
public class MerchantApiMerchantController {

    @Resource
    private IMerchantService merchantService;

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public Result<PageInfo<MerchantQueryRes>> queryPage(@RequestBody MerchantQueryReq params) {
        PageInfo<MerchantQueryRes> pageInfo = merchantService.queryPage(params);
        return Result.success(pageInfo);
    }

    /**
     * 查询当前登录用户的商户
     *
     * @param merchantCode 商户编码，登录返回
     * @return
     */
    @RequestMapping(value = "/currentMerchant", method = RequestMethod.GET)
    @ResponseBody
    public Result<QueryCurrentMerchantRes> currentMerchant() {
        final PayMerchantDTO merchant = MerchantContext.getMerchant();
        QueryCurrentMerchantRes res = merchantService.currentMerchant(merchant.getMerchantCode());
        return Result.success(res);
    }

    /**
     * 查询商户基础信息
     *
     * @param platformMerchantId 平台商户关联id
     * @return
     */
    @RequestMapping(value = "/queryMerchantBaseInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result<MerchantAddStepOneRes> queryByMerchantCode(@RequestParam("platformMerchantId") Long platformMerchantId) {
        MerchantAddStepOneRes res = merchantService.queryByPlatformMerchantId(platformMerchantId);
        return Result.success(res);
    }

    /**
     * 查询商户业务信息
     *
     * @param platformMerchantId 平台商户id
     * @return
     */
    @RequestMapping(value = "/queryMerchantBusinessInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result<PlatformMerchantBusinessInfoRes> queryMerchantBusinessInfo(@RequestParam("platformMerchantId") Long platformMerchantId) {
        PlatformMerchantBusinessInfoRes res = merchantService.queryMerchantBusinessInfo(platformMerchantId);
        return Result.success(res);
    }

    /**
     * 更换商户管理员
     *
     * @param setMerchantAdminReq 平台商户id
     * @return
     */
    @RequestMapping(value = "/changeMerchantAdmin", method = RequestMethod.POST)
    @ResponseBody
    public Result<PlatformMerchantUserDetailDTO> changeMerchantAdmin(@RequestBody @Valid SetMerchantAdminReq setMerchantAdminReq,HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        setMerchantAdminReq.setXToke(token);
        PlatformMerchantUserDetailDTO res = merchantService.changeMerchantAdmin(setMerchantAdminReq);
        return Result.success(res);
    }

    // ----------------------------------------------------------------------------平台商户状态管理--------------------------------------------------------------

    /**
     * 商户入驻：第一步，添加商户信息
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/addMerchant/v2", method = RequestMethod.POST)
    @ResponseBody
    public Result<MerchantAddStepOneNewRes> addMerchant(@RequestBody @Valid MerchantAddReqNew params, HttpServletRequest servletRequest) {
        String xToken = servletRequest.getHeader("X-Token");
        log.info("xToken: {}", xToken);
        params.setXToken(xToken);
        MerchantAddStepOneNewRes add = merchantService.addNew(params);
        return Result.success(add);
    }

    /**
     * 更新商户信息
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateMerchantInfo/v2", method = RequestMethod.POST)
    @ResponseBody
    public Result updateMerchantInfo(@RequestBody @Valid MerchantUpdateNewReq params, HttpServletRequest servletRequest) {
        String xToken = servletRequest.getHeader("X-Token");
        log.info("xToken: {}", xToken);
        params.setXToken(xToken);
        merchantService.updateMerchantInfoNew(params);
        return Result.success();
    }

    /**
     * 商户对公打款验证
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/merchantAccountVerify", method = RequestMethod.POST)
    @ResponseBody
    public Result merchantAccountVerify(@RequestBody @Valid MerchantAccountVerifyReq params) {
        merchantService.merchantAccountVerify(params);
        return Result.success("对公账户验证申请成功，请到对应对公账户查看打款金额！");
    }

    /**
     * 发起对公账户验证交易
     *
     * @param param
     * @return
     */
    @PostMapping("/companyAccountVerify")
    public Result companyAccountVerify(@RequestBody @Validated MerchantCompanyAccountVerifyReq param) {
        merchantService.companyAccountVerify(param);
        return Result.success("对公账户打款金额验证成功！");
    }

    /**
     * 前台签约接口
     *
     * @param param
     * @return
     */
    @PostMapping("/agreementSign")
    @ResponseBody
    public Result<String> agreementSign(@RequestBody @Validated MerchantPayAgreementSignReq param) {
        String url = merchantService.agreementSign(param);
        return Result.success(url);
    }

}
